/**
 * 时时彩的共用逻辑
 */
(function() {
    var currTabId = 1;
    var tabDraws = [
        // 1：两面
        fill_zonghe_row,

        // 2-6:5球的单号
        function() {
            fill_table(1);
        },
        function() {
            fill_table(2);
        },
        function() {
            fill_table(3);
        },
        function() {
            fill_table(4);
        },
        function() {
            fill_table(5);
        },
    ];

    // 填充总和
    function fill_zonghe_row() {
        var table = $(".zonghe tbody");
        table.empty();

        var types = ["大", "小", "单", "双"];
        var tr = $("<tr></tr>");
        for (var i = 0; i < types.length; i++) {
            var t = types[i];
            add_bet_item('和' + t, '总和' + t, tr);
        }
        table.append(tr);

        var tr = $("<tr></tr>");
        var types = ["龙", "虎", "和"];
        for (var i = 0; i < types.length; i++) {
            var t = types[i];
            add_bet_item(t, t, tr);
        }
        tr.append("<td colspan='3'></td>");
        table.append(tr);

        // 前三
        var types = ['豹子', '顺子', '对子', '半顺', '杂六'];
        var table = $(".qian3 tbody");
        table.empty();
        var tr = $("<tr></tr>");
        for (var i = 0; i < types.length; i++) {
            var t = types[i];
            add_bet_item("前" + t[0], t, tr);
        }
        table.append(tr);

        // 中三
        var table = $(".zhong3 tbody");
        table.empty();
        var tr = $("<tr></tr>");
        for (var i = 0; i < types.length; i++) {
            var t = types[i];
            add_bet_item("中" + t[0], t, tr);
        }
        table.append(tr);

        // 后三
        var table = $(".hou3 tbody");
        table.empty();
        var tr = $("<tr></tr>");
        for (var i = 0; i < types.length; i++) {
            var t = types[i];
            add_bet_item("后" + t[0], t, tr);
        }
        table.append(tr);  

        // 5球的大小单双、数字
        var div = $(".q1-5 tbody tr");
        div.empty();
        var arr = ['一', '二', '三', '四', '五'];
        var types = ["大", "小", "单", "双"];
        for (var j in arr) {
            var table = $('<table class="u-table2 play_tab_2"></table>');
            table.append('<thead><tr><th colspan="3">第__i__球</th></tr></thead>'.replace(/__i__/, arr[j]));
            var tbody = $("<tbody></tbody>");
            table.append(tbody);

            for (var i = 0; i < types.length; i++) {
                var tr = $("<tr></tr>");
                var t = types[i];
                add_bet_item(arr[j] + t, t, tr);
                tbody.append(tr);
            }

            for (var i = 0; i <= 9; i++) {
                var tr = $("<tr></tr>");
                var title = '<span class="ball c-b1">__i__</span>'.replace(/__i__/, i);
                add_bet_item(arr[j] + i, title, tr);
                tbody.append(tr);
            }

            var td = $("<td></td>");
            td.append(table);
            div.append(td);
        }
    }

    // 填充投注区域表格
    function fill_table(index) {
        // 填充单号
        var table = $(".q" + index + " tbody");
        var arr = ['一', '二', '三', '四', '五'];
        var types = ["大", "小", "单", "双"];

        // 0~9球的数字
        {
            var z = index - 1;
            table.empty();
            var tr = $("<tr></tr>");
            for (var i = 0; i <= 4; i++) {
                var title = '<span class="ball c-b1">__i__</span>'.replace(/__i__/, i);
                add_bet_item(arr[z] + i, title, tr);
            }
            table.append(tr);

            var tr = $("<tr></tr>");
            for (var i = 5; i <= 9; i++) {
                var title = '<span class="ball c-b1">__i__</span>'.replace(/__i__/, i);
                add_bet_item(arr[z] + i, title, tr);
            }
            table.append(tr);

            // 大小单双
            var tr = $("<tr></tr>");
            for (var i = 0; i < types.length; i++) {
                var t = types[i];
                add_bet_item(arr[z] + t, t, tr);
            }
            tr.append("<td colspan='3'> </td>");
            table.append(tr);
        }
    }

    var initF = function(game, first) {
        window.game = game;
        initGame(game, function() {
            if (first !== false) fillGameTabs(["整合", "第一球", "第二球", "第三球", "第四球", "第五球"]);
            currTabId = parseInt($("#cate_menus_50 .selected").attr("tid"));
            $("#tab" + currTabId).attr("flag", 1);
            var f = tabDraws[currTabId - 1];
            f();
        }, first);
    }

    // 绘制开奖结果
    var drawF = function(list) {
        var div = $("#result_balls");
        div.attr("class", "T_SSC L_CQSSC");
        var l = '<span><b class="b__index__"></b></span>';
        for (var i in list) {
            var s = l.replace(/__index__/g, parseInt(list[i]));
            div.append(s);
        }
    }

    // 检测能否下注，看下单号1-10是否满足要求
    var checkF = function(type, issue, data) {
        if (type != 'cqssc' && type != 'xjssc' && type != 'tjssc' && type != 'zyssc') return true;

        var limit = Util.getNumberLimit(type);
        if (limit <= 0 || limit >= 10) return true;

        var arr1 = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十'];
        var arr2 = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
        var m = {};
        for (var k in data) {
            if (k.length != 2) continue;
            if (arr1.indexOf(k[0]) != -1 && arr2.indexOf(k[1]) != -1) {
                if (!m[k[0]]) m[k[0]] = {};
                m[k[0]][k[1]] = true;
            }
        }
        if (Util.isEmpty(m)) return true;

        // 获取下已经下注的单据
        var bets = context.bets[type];
        if (bets) bets = bets[issue] || [];
        else bets = [];
        for (var i in bets) {
            var k = bets[i].target;
            if (k.length != 2) continue;
            if (arr1.indexOf(k[0]) != -1 && arr2.indexOf(k[1]) != -1) {
                if (!m[k[0]]) m[k[0]] = {};
                m[k[0]][k[1]] = true;
            }
        }

        var total = 0;

        for (var k in m) {
            var m2 = m[k];
            if (Object.keys(m2).length > limit) {
                var l = "下注失败，__g__ 定位玩法，单个名次不允许下注__v__码或者__v__以上！如有疑问，请咨询客服！";
                l = l.replace(/__g__/, get_game_display_name(type));
                l = l.replace(/__v__/g, limit);
                alert2(l, true);
                return false;
            }
        }

        return true;
    } 
    var data = {
        drawResult: drawF,
        checkBetList: checkF,
        init: initF,
    }
    context.global.game["cqssc"] = data;
    context.global.game["tjssc"] = data;
    context.global.game["xjssc"] = data;
    context.global.game["zyssc"] = data;
    context.global.game["ffcqq"] = data;
})();